﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BLL;
using BLL.Validation;
using DAL;
using Entities;
using System.Linq;
using WindowsApp.QLMonHoc;
using DAL.IRepository;

namespace WindowsApp.QLMonHoc
{
    public partial class frmQLMonHoc : DevComponents.DotNetBar.Office2007Form
    {
        private IGeneralRepository<MonHoc> _repository;
        private MonHocManager _manager;
        public TaiKhoan _tk;
        private bool _selectAll = false;
        public frmQLMonHoc()
        {
            InitializeComponent();
            dgvDSMonHoc.AutoGenerateColumns = false;
        }

        private void frmQLMonHoc_Load(object sender, EventArgs e)
        {
            _repository = new GeneralRepository<MonHoc>();
            _manager = new MonHocManager(_repository);
            dgvDSMonHoc.DataSource = _repository.GetAll().ToList();
            XuLyPhanQuyen();
        }

        void XuLyPhanQuyen()
        {
            if (_tk.PhanQuyen.ToUpper() == "ADMIN")
            {
                btnThem.Enabled = true;
                btnXoa.Enabled = true;
                btnSua.Enabled = true;
                mnuThemMH.Enabled = true;
                mnuSuaMH.Enabled = true;
                mnuXoaMH.Enabled = true;
            }
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            dgvDSMonHoc.DataSource = _repository.GetAll().ToList() ;
            txtTenMH.ResetText();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            var frm = new frmAddMonHoc(_manager,this);
            frm.Text = "Thêm môn học";
            frm.ShowDialog();
            dgvDSMonHoc.DataSource = _repository.GetAll().ToList();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn xóa các môn học đã chọn?", "Xóa môn học", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.Cancel)
                return;
            foreach (DataGridViewRow row in dgvDSMonHoc.Rows)
            {
                if (row.Cells[0].Value != null && row.Cells[0].Value.Equals(true))
                    _manager.Delete(row.Cells[1].Value.ToString());
            }
            dgvDSMonHoc.DataSource = _repository.GetAll().ToList();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            var aResult = _manager.Find(s => s.TenMH.IndexOf(txtTenMH.Text) >= 0);
            if (aResult.Success)
                dgvDSMonHoc.DataSource = aResult.Result;
            else
            {
                MessageBox.Show(aResult.GetErrorMessages());
                btnLamMoi.PerformClick();
                txtTenMH.ResetText();
                txtTenMH.Focus();
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (dgvDSMonHoc.SelectedRows.Count > 0)
            {
                MonHoc mh = new MonHoc();
                DataGridViewRow row = dgvDSMonHoc.SelectedRows[0];
                mh.MaMH = row.Cells[1].Value.ToString();
                mh.TenMH = row.Cells[2].Value.ToString();
                mh.SoTCLT = (int)row.Cells[4].Value;
                mh.SoTCTH = (int)row.Cells[5].Value;
                mh.BatBuoc = (bool)row.Cells[6].Value;
                var frm = new frmAddMonHoc(_manager,this, mh);
                frm.Text = "Cập nhật môn học";
                frm.ShowDialog();
                dgvDSMonHoc.DataSource = _repository.GetAll().ToList();
            }
            else
                MessageBox.Show("Chưa chọn môn học để sửa","Thông báo");

        }

        private void dgvDSMonHoc_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == 0 && dgvDSMonHoc.Rows.Count > 0)
            {
                _selectAll = !_selectAll;
                for (int idx = 0; idx < dgvDSMonHoc.Rows.Count; idx++)
                {
                    dgvDSMonHoc.Rows[idx].Selected = _selectAll;
                    dgvDSMonHoc.Rows[idx].Cells[0].Value = _selectAll;
                }
            }
        }

        private void dgvDSMonHoc_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && dgvDSMonHoc.Rows[e.RowIndex].Cells[0].Value != null)
            {
                bool selected = (bool)dgvDSMonHoc.Rows[e.RowIndex].Cells[0].Value;
                dgvDSMonHoc.Rows[e.RowIndex].Selected = selected;
            }
        }

        private void txtTenMH_TextChanged(object sender, EventArgs e)
        {
            var aResult = _manager.Find(s => s.TenMH.IndexOf(txtTenMH.Text) >= 0);
            if (aResult.Success)
                dgvDSMonHoc.DataSource = aResult.Result;
            else
                dgvDSMonHoc.DataSource = null;
        }
    }
}